$(function(){
  var max_len = $(".maxNum").text() || 5 ;
  var min_len = $(".minNum").text() || 1 ;
  $(".plus").on("click",function(){
    var past_num = parseInt ($(this).parents(".za-count").find(".ct_num").val());
    var new_num = past_num - 1;
    if( past_num <= min_len ) {
      App.showToast("当前险种，请至少购买"+min_len+"份");
      $(this).parents(".za-count").find(".ct_num").val( past_num );
    }else{
      $(this).parents(".za-count").find(".ct_num").val( new_num );
    }
  });
  $(".add").on("click",function(){
    var past_num = parseInt ($(this).parents(".za-count").find(".ct_num").val());
    var new_num = past_num + 1;
    if( past_num >= max_len) {
      App.showToast("当前险种，限定只能每人购买"+max_len+"份");
      $(this).parents(".za-count").find(".ct_num").val( past_num );
    }else{
      $(this).parents(".za-count").find(".ct_num").val( new_num );
    }
  });
  $('.project-select').on("change", function(){
    projectCentect();
  });
  /*进入详情页，默认选中第一个保障计划*/
  projectCentect();
});
/**
 * form表单
 */
seajs.use(["zui/form","zui/fightCalendar","zui/date"],function(Form,Calendar,zDate){
  var form1 = 
      new Form({srcNode:"#CM_Form",validType:"custom",validateFunc:function(ev){
        var field=ev.field;
        var msg=ev.validateMsg;
        if(msg==""){

        }else{
            App.showToast(msg);
          }
        }});
  $(".zaw-btn").on("click",function(){
    if(form1.valid()){
        $("#CM_Form").submit();
    }
  });
});
/*计算金额*/
function cost(n){
  var cost = $(".project-select option").eq(n).attr("za-cost");
  $("#cost").val(cost);
}
/*保障计划关联保险额度*/
function projectCentect(){
    var _target = $(".project-select option").attr("selected");
    var index;
    $(".project-select option").each(function(){
      if( $(this).attr("selected") == true ){
         index = $(this).index();
      }
    });
    $('.prd-table-box').hide().eq(index).show();
    cost(index);
}